#!/usr/bin/env python
# -*- coding: utf-8 -*-

# @Time     :2020/09/29
# @Author   :Changshu
# @File     :practice_202.py
# 202 快乐数

#参考得到：使用快慢指针的方法，如果不是快乐数，那么一定会陷入一个 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 的循环中，那么快慢指针就会相遇
#						  如果是块乐数，快慢指针最后都会是1，也会相等
def isHappy(n:int)->bool:
	slow=squareSum(n)
	fast=squareSum(squareSum(n))
	while fast!=slow:
		slow = squareSum(slow)
		fast = squareSum(squareSum(fast))
	if slow==1:
		return True
	return False

def squareSum(n:int)->int:
	num=str(n)
	sum=0
	for i in num:
		t=int(i)
		sum+=t**2
	return sum

if __name__ == '__main__':
	n=67
	# s=squareSum(n)
	b=isHappy(n)
	print(b)